package com.situ.mall.controller;


import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.mall.pojo.dto.OrderDTO;
import com.situ.mall.pojo.entity.OrderItem;
import com.situ.mall.pojo.entity.Orders;
import com.situ.mall.pojo.query.OrderQuery;
import com.situ.mall.pojo.vo.OrderVO;
import com.situ.mall.service.IOrdersService;
import com.situ.mall.util.JwtUtil;
import com.situ.mall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-10-15
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private IOrdersService ordersService;

    @GetMapping("/selectListByUser")
    public Result<List<OrderVO>> selectListByUser(@RequestHeader(name = "Authorization")String token) {
        Map<String, Object> tokenMap = JwtUtil.parseToken(token);
        Integer userId = (Integer) tokenMap.get("id");
        List<OrderVO> list = ordersService.selectListByUser(userId);
        return Result.ok(list);
    }


    @PutMapping("/pay/{orderNo}")
    public Result pay(@PathVariable Long orderNo) {
        ordersService.pay(orderNo);
        return Result.ok("支付成功");
    }

    @GetMapping("/selectById/{orderNo}")
    public Result<Order> selectById(@PathVariable Long orderNo) {
        Orders orders = ordersService.getById(orderNo);
        return Result.ok(orders);
    }

    @PostMapping("/add")
    public Result add(@RequestBody OrderDTO orderDTO, @RequestHeader(name = "Authorization")String token) {
        Map<String, Object> tokenMap = JwtUtil.parseToken(token);
        Integer userId = (Integer) tokenMap.get("id");
        orderDTO.setUserId(userId);
        Long orderNo = ordersService.add(orderDTO);
        return Result.ok("添加成功", orderNo);
    }

    @GetMapping("/list")
    public Result<IPage<Orders>> list(OrderQuery orderQuery) {
        System.out.println("OrderController.list");
        IPage<Orders> page = ordersService.list(orderQuery);
        return Result.ok(page);
    }

    // /deleteById?id=1
    // /deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        ordersService.removeById(id);
        return Result.ok("删除成功");
    }

    // /deleteAll/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        ordersService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return Result.ok("更新成功");
    }

    @GetMapping("/selectOrderItemByOrderNo/{orderNo}")
    public Result<List<OrderItem>> selectOrderItemByOrderNo(@PathVariable Long orderNo) {
        List<OrderItem> list = ordersService.selectOrderItemByOrderNo(orderNo);
        return Result.ok(list);
    }
}

